<?php
namespace data\sys;
class ConfigData extends \data\BaseData{
    
    /**
     * 读取一条信息
     */
    public function find($param){
        $fields = ['id','type','siteid','tag'];
        $where = $this->parseWhere($fields,$param);
        $info  = M('sys_config')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一条信息
     */
    public function add($data){        
        $check = [
            'type'      => ['check'=>'must','msg'=> '类型不能为空','reg'=> [1,2,3]],
            'siteid'    => ['def'=>0,'reg'=> 'int'],
            'tag'       => ['check'=>'must','msg'=> '配置标识不能为空'],
            'name'      => ['check'=>'must','msg'=> '配置名称不能为空'],
            'note'      => '',
            'content'   => '',
            'edittime'  => ['val'=>SYS_TIME],
        ];
        $save = $this->verifyData($check,$data);
        return M('sys_config')->insertGetId($save);
    }
    
    /**
     * 更新一条信息
     */
    public function edit($id,$data){
        $check = [
            'type'      => ['reg'=> [1,2,3]],
            'siteid'    => ['reg'=> 'int'],
            'tag'       => ['reg'=> 'must'],
            'name'      => ['reg'=> 'must'],
            'note'      => '',
            'content'   => '',
            'edittime'  => ['val'=>SYS_TIME],
        ];
        $save = $this->verifyData($check,$data);
        $where = ['id'=>$id];
        $result = M('sys_config')->where($where)->update($save);
        if( $result === false ) dataE('更新配置失败');
        return $result;
    }
    
    /**
     * 读取列表
     */
    public function getList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:20;
        $check = ['type'];
        $where = $this->parseWhere($check,$param);
        $list =  M('sys_config')->where($where)->order('id desc')->limit($limit)->page($page)->select();
        $total =  M('sys_config')->where($where)->count();
        return [            
            'page' => $page,
            'limit' => $limit,
            'total'=> $total,
            'list' => $list,
        ];
        
    }
    
}